<?php
include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */
if ($_GET['id_ad'] == "0")
{
    if ($_POST['click'] == "1")
    {
        $cc1 = intval($_POST['cc']);
        $i=0;
        while ($cc1>$i)
        {
            $i=$i+1;
            $ffield_id = 'field_id'.$i;
            $iid = 'fffid'.$i;
            $oordering = 'ordering'.$i;
            // echo $ffield_id.'='.$_POST[$ffield_id].','.$iid.'='.$_POST[$iid].'<br>';
            $sql_qq = 'SELECT * FROM field_search WHERE category_action_id='.intval($_POST['category_action_id']).' AND field_id='.intval($_POST[$ffield_id]).' LIMIT 1';
            $result_qq = $result = mysqli_query($con, $sql_qq);
            $row_qq = mysqli_fetch_array($result_qq);
            $found = 0;
            if ($_POST[$ffield_id] != "")
            {
                //    echo $sql_qq.'<br>';
                if (empty($row_qq)){
                    $found_f =0;
                }
                else
                {
                    $found_f = 1;
                }
                if ($found_f == 1)
                {
                 //       echo $oordering.'='.$_POST[$oordering].'<br>';
                    $sql_q = 'UPDATE field_search SET field_id='.intval($_POST[$ffield_id]).',category_action_id='.intval($_POST['category_action_id']).',ordering='.intval($_POST[$oordering]).' WHERE id='.intval($row_qq['id']);
                    $result = mysqli_query($con, $sql_q);
                    //echo $sql_q.'<br>';
                }
                else
                {
                    $sql_q = 'INSERT INTO field_search (`id`,`field_id`,`category_action_id`,`ordering`) VALUES (NULL,'.intval($_POST[$ffield_id]).','.intval($_POST['category_action_id']).','.intval($_POST[$oordering]).')';
                 //    echo $sql_q.'<br>';
                    $result = mysqli_query($con, $sql_q);
                }
            }
            else
            {
                $sql_q = 'DELETE FROM field_search WHERE category_action_id='.intval($_POST['category_action_id']).' AND field_id='.intval($_POST[$iid]);
                // echo $sql_q.'<br>';
                $result = mysqli_query($con, $sql_q);
            }
        }
    }

    $click = "0";
    $_POST["click"] = "0";
    //if (!ISSET($_POST['parent_id']))
    //{
    //    $_POST['parent_id'] = 0;
    // }
    ?>
    <script>
        function my_yoz(){
            document.form_field_category_action_search.click.value='0';
            document.getElementById("form_field_category_action_search").submit();
        }
    </script>
    <?php
    $form = '<h4>Αντιστοίχηση πεδίων - κατηγοριών - αναζήτησης</h4>';
    $form = $form . '<form name="form_field_category_action_search" id="form_field_category_action_search" method="post">';
    $form = $form . '<label>Κατηγορία - Ενέργια - αναζήτηση</label>';
    $form = $form . '<select name="category_action_id" onchange="my_yoz()"/>';
    $form = $form . '<option selected value="">Επιλέξτε</option>';
    $sql_1 = "SELECT category_action.id as caid,category_action.category_id,category_action.action_group_id,category.name as cn,category.id,action_group.id, action_group.name as agn FROM category_action,category,action_group WHERE category_action.category_id = category.id AND category_action.action_group_id = action_group.id group by category.name";
    $result1 = mysqli_query($con, $sql_1);
    //echo $sql_1;
    $_POST["click"] = "0";
    // echo $_POST["click"];
    while($row = mysqli_fetch_array($result1))
    {
        if (intval($_POST['category_action_id']) == $row['caid'])
        {
            $form = $form . '<option selected value="'.$row['caid'].'">'.$row['cn'].'</option>';
        }
        else{
            $form = $form . '<option value="'.$row['caid'].'">'.$row['cn'].'</option>';
        }
    }
    $form = $form . '</select>';
    if (!empty($_POST['category_action_id'])){
        $form = $form . '<label>Πεδία</label>';
//    $form = $form . '<select name="field_id"/>';
// $form = $form . '<option value="0" selected="selected"</option>';
        $sql_ = "SELECT field.id,field.name,field_matrix.parent_id FROM field,field_matrix where field.parameters <> 'NULL' group by field.name";
        $result2 = mysqli_query($con, $sql_);
        $cc=0;
        while($row = mysqli_fetch_array($result2))
        {
            $sql_3 = 'SELECT field_search.id as fid, field_search.field_id as fmfid, field_search.category_action_id as fmpi, field_search.ordering as fmo FROM field_search where field_search.field_id='.intval($row['id']).' AND field_search.category_action_id='.intval($_POST['category_action_id']);
            // echo $sql_3.'<br>';
            $result_3 = mysqli_query($con, $sql_3);
            $row3 = mysqli_fetch_array($result_3);
            $cc=$cc+1;
            if (empty($row3))
            {
                $form = $form . '<input type="checkbox" name ="field_id'.$cc.'" value="'.$row['id'].'" /> '.$row['name'].' Ταξινόμηση ';
                $form = $form . '<input type="text" name="ordering'.$cc.'" value="'.$row3['fmo'].'" class="input-mini"/><br>';
                $form = $form . '<input name="id'.$cc.'" type="hidden" value="0"/>';
                $form = $form . '<input name="fffid'.$cc.'" type="hidden" value="'.$row['id'].'"/>';
            }
            else
            {
                $form = $form . '<input type="checkbox" name ="field_id'.$cc.'" value="'.$row['id'].'" checked/> '.$row['name'].' Ταξινόμηση ';
                $form = $form . '<input type="text" name="ordering'.$cc.'" value="'.$row3['fmo'].'" class="input-mini"/><br>';
                $form = $form . '<input name="id'.$cc.'" type="hidden" value="'.$row3['fid'].'"/>';
                $form = $form . '<input name="fffid'.$cc.'" type="hidden" value="'.$row['id'].'"/>';
            }
        }
        $form = $form . '<input name="cc" type="hidden" value="'.$cc.'"/>';
        $form = $form . '<input name="click" type="hidden" value="1"/>';
        $form = $form . '<input type="submit" value="Αντιστοίχιση"/>';
        $form = $form . '</form>';

    }
    echo $form;
}
elseif ($_GET['id_ad'] == "1") {
    $sql_2 = "SELECT field.id AS aid, field.name AS aname, field_matrix.id as bid, field_matrix.field_id AS bcid, field_matrix.parent_id AS bparent_id, field_matrix.ordering AS bordering FROM  field JOIN field_matrix WHERE field.id = field_matrix.field_id GROUP BY field_matrix.id";
    $result = mysqli_query($con, $sql_2);
    echo '<table border="1">';
    echo '<tr><td>Id</td><td>Field id</td><td>Name</td><td>Parent_id</td><td>Ordering</td></tr>';
    while($row = mysqli_fetch_array($result))
    {
        echo '<tr><td>'.$row['bid'].'</td><td>'.$row['bcid'].'</td><td>'.$row['aname'].'</td><td>'.$row['bparent_id'].'</td><td>'.$row['bordering'].'</td><td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=2&id='.$row['bid'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=3&id='.$row['bid'].'">Διαγραφή</a> ]</td></tr>';
    }
    echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>

